How does this JavaScript/JQuery Syntax: (function( window, undefined ) { })(window) work?

Posted by DKinzer on Stack Overflow See other posts from Stack Overflow or by DKinzer
Published on 2010-04-26T18:58:59Z Indexed on 2010/04/26 19:03 UTC
Read the original article Hit count: 450

Filed under:
|
|

Have you ever taken a look under the hood at the JQuery 1.4 source code and noticed how it's encapsulated in the following way:

(function( window, undefined ) {

  //All the JQuery code here 
  ...

})(window);

I've read an article on JavaScript Namespacing and another one called "An Important Pair of Parans," so I know some about what's going on here.

But I've never seen this particular syntax before. What is that undefined doing there? And why does window need to be passed and then appear at the end again?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery